package com.example.studyLlm;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Flux;

@Service
public class ChatServiceImpl extends ServiceImpl<CallRecordMapper, CallRecord> implements ChatService {
    private final WebClient webClient = WebClient.create();

    @Override
    public Flux<Response> sendMessage(Request request) {
        return webClient.post()
            .uri("http://8.220.140.148/v1/chat-messages")
            .header("Authorization", "Bearer {api_key}")
            .header("Content-Type", "application/json")
            .bodyValue(request)
            .retrieve()
            .bodyToFlux(Response.class)
            .doOnNext(response -> {
                CallRecord record = new CallRecord();
                record.setQuery(request.getQuery());
                record.setResponseMode(request.getResponseMode());
                record.setConversationId(request.getConversationId());
                record.setUser(request.getUser());
                record.setStatus(response.getStatus());
                record.setMessage(response.getMessage());
                record.setCreateTime(LocalDateTime.now());
                this.save(record);
            });
    }
}